package com.bloveoacom.weight;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.util.StringUtils;
import com.bloveoacom.util.ToastUtil;
import com.bloveoacom.weichi.R;


public class InputDialog extends Dialog implements View.OnClickListener {
    private TextView tv_title;
    private EditText et_content;
    private RelativeLayout rl_left, rl_right;
    private ItemClickListener itemClickListener;
    private String title, hint;
    private boolean isPassword,isNumber;
    private String content;

    public InputDialog(Context context, String title, String hint, boolean isPassword, boolean isNumber, int theme) {
        super(context, theme);
        this.title = title;
        this.hint = hint;
        this.isPassword = isPassword;
        this.isNumber = isNumber;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_input);
        this.setCanceledOnTouchOutside(true);
        tv_title = (TextView) findViewById(R.id.tv_title);
        rl_left = (RelativeLayout) findViewById(R.id.rl_left);
        rl_right = (RelativeLayout) findViewById(R.id.rl_right);
        et_content = (EditText) findViewById(R.id.et_content);
        //设置内容
        tv_title.setText(title);
        et_content.setHint(hint);
        if (isPassword) {
            et_content.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
        if (isNumber) {
            et_content.setInputType(InputType.TYPE_CLASS_NUMBER);
        }

        //设置监听
        rl_right.setOnClickListener(this);
        rl_left.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        content = et_content.getText().toString();
        switch (v.getId()) {
            case R.id.rl_right:
                if (itemClickListener == null) {
                    return;
                }
                if (TextUtils.isEmpty(content)) {
                    ToastUtil.makeShortText(getContext(), "请输入内容");
                    return;
                }
                dismiss();
                itemClickListener.right(content);
                break;
            case R.id.rl_left:
                if (itemClickListener != null) {
                    dismiss();
                    itemClickListener.left();
                }
                break;
            default:
                break;
        }
    }

    public void clearContent(){
        if(et_content!=null){
            et_content.setText("");
        }
    }

    public interface ItemClickListener {
        void left();

        void right(String content);
    }

    @Override
    public void show() {
        if(isPassword&& StringUtils.isEmpty(UserInfoBean.getInstance().getWalletAccount())){
            ToastUtils.showShort("请绑定钱包");
            return;
        }
        super.show();
    }
}
